package tests;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import static com.google.appengine.api.datastore.FetchOptions.Builder.*;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import edu.idei.anteweb.repositories.PersonaRepository;

public class PersonaRepositoryTest {

	private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig());

	private PersonaRepository pr = new PersonaRepository();

	@Before
	public void setUp() {
		helper.setUp();
	}

	@After
	public void tearDown() {
		helper.tearDown();
	}

	// run this test twice to prove we're not leaking any state across tests
	private void doInsert() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		assertEquals(0,
				ds.prepare(new Query("Persona")).countEntities(withLimit(10)));
		pr.addPersona("Ormeño", "Emilio Gustavo");
		pr.addPersona("Alvarez", "Sandra Mónica");
		pr.addPersona("Zapata", "Sergio Gustavo");
		pr.addPersona("Díaz", "Daniel");
		assertEquals(4,
				ds.prepare(new Query("Persona")).countEntities(withLimit(10)));
	}

	@Test
	public void testInsert1() {
		doInsert();
	}

	@Test
	public void testInsert2() {
		doInsert();
	}
}